<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
  <meta name="baidu_union_verify" content="0423ec0cdca4f8eebe6e059172fc0e94">
  <meta name="baidu-site-verification" content="code-pR8Cu8CCAR" />
  <meta name="google-site-verification" content="kPx7uwBYN8ZNVw-1hPQgk5yziYYGu7ZawLlYvgTblwQ" />
  <meta name="sogou_site_verification" content="bt3Su26Lme"/>
  <meta name="360-site-verification" content="73ba99e99d3fc1e5f8a37d8c1d998703" />
   
  <meta name="keywords" content="LIEFox,liefox,liefox拾柒,LIEFox拾柒,编程笔记,JAVA,前端" />
   
  <meta name="description" content="LIEFox一个有趣又好玩的学习编程网站" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Java web 开发 |  LIEFox
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  
<script>
var _hmt = _hmt || [];
(function() {
	var hm = document.createElement("script");
	hm.src = "https://hm.baidu.com/hm.js?70837313b75a2b48b1e427bcabe5c0d9";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>



<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>

<!-- 封面标闪烁 -->
<link rel="stylesheet" href="/css/zhyBlogTitle.css">

<!-- Highlight.js -->
<link href="https://cdn.bootcdn.net/ajax/libs/highlight.js/9.18.1/styles/a11y-dark.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>

</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-Web基础"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Java web 开发
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/posts/f1404f68.html" class="article-date">
  <time datetime="2021-03-14T16:00:00.000Z" itemprop="datePublished">2021-03-15</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/JAVA%E7%AC%94%E8%AE%B0/">JAVA笔记</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">7.3k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">35 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p><img src="https://i.loli.net/2021/03/15/t2ZzYDpvhnOksJ3.png" alt="image-20210315132611221"></p>
<a id="more"></a>

<h1 id="Java-web-开发"><a href="#Java-web-开发" class="headerlink" title="Java web 开发"></a>Java web 开发</h1><h2 id="web前言"><a href="#web前言" class="headerlink" title="web前言"></a>web前言</h2><ul>
<li><p>静态网页</p>
<ul>
<li>html+css+JavaScript</li>
<li>网页所提供的数据不会实时更新，并且不会与数据库进行交互</li>
<li><img src="https://i.loli.net/2021/03/15/iNVgfUReXCW7yak.png" alt="image-20210315130443959"></li>
</ul>
</li>
<li><p>动态网页</p>
<ul>
<li>JSP/Servlet，ASP，PHP</li>
<li>网页所提供的数据会实时更新，并且与数据库进行交互</li>
<li><img src="https://i.loli.net/2021/03/15/16nFCIKSc4t7zks.png" alt="image-20210315124135411"></li>
</ul>
</li>
<li><p>JSP（基于B/S架构）</p>
<ul>
<li>sun公司主推的B/S架构</li>
<li>基于Java语言的</li>
<li>可以承载三高问题</li>
</ul>
</li>
</ul>
<h2 id="Web服务器"><a href="#Web服务器" class="headerlink" title="Web服务器"></a>Web服务器</h2><h3 id="Tomcat"><a href="#Tomcat" class="headerlink" title="Tomcat"></a>Tomcat</h3><p><img src="https://i.loli.net/2021/03/15/t2ZzYDpvhnOksJ3.png" alt="image-20210315132611221"></p>
<p>Tomcat是阿帕奇基金会推出的Web 应用服务器，是免费且适合初学Java web的人来使用的。</p>
<h3 id="Tomcat下载"><a href="#Tomcat下载" class="headerlink" title="Tomcat下载"></a>Tomcat下载</h3><blockquote>
<ul>
<li>Tomcat官网：<a target="_blank" rel="noopener" href="https://tomcat.apache.org/">https://tomcat.apache.org/</a></li>
<li>找到最对应的tomcat版本（我的是9.0.37)区别不大</li>
<li><img src="https://i.loli.net/2021/03/15/pOuTx4JRM2s87Sa.png"></li>
</ul>
</blockquote>
<blockquote>
<ul>
<li><img src="https://i.loli.net/2021/03/15/Th9ImXgvRONtJcY.png" alt="image-20210315150426047"></li>
<li>然后解压缩后得到一个文件夹，不要去改动文件名<img src="https://i.loli.net/2021/03/15/skMI2KeAmPgoB7u.png"></li>
</ul>
</blockquote>
<h3 id="Tomcat各文件夹详讲"><a href="#Tomcat各文件夹详讲" class="headerlink" title="Tomcat各文件夹详讲"></a>Tomcat各文件夹详讲</h3><blockquote>
<ul>
<li><h4 id="bin文件夹"><a href="#bin文件夹" class="headerlink" title="bin文件夹"></a>bin文件夹</h4></li>
<li><p><img src="https://i.loli.net/2021/03/15/Qkeob5sTtu3HG1X.png" alt="image-20210315151249374"></p>
</li>
<li><p>双击启动tomcat<img src="https://i.loli.net/2021/03/15/eq6EWmsfcGuDUOr.png" alt="image-20210315153720190"></p>
</li>
<li><p>这个时候我们就可以去浏览器访问<a target="_blank" rel="noopener" href="http://localhost:8080/">http://localhost:8080/</a></p>
</li>
</ul>
<p><img src="https://i.loli.net/2021/03/15/2CazNTLonKXF1S6.png"></p>
</blockquote>
<blockquote>
<ul>
<li>关闭同理，关闭黑窗口或运行关闭脚本</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li><h4 id="conf文件夹"><a href="#conf文件夹" class="headerlink" title="conf文件夹"></a>conf文件夹</h4></li>
<li><p>最重要的就是 <code>server.xml</code>文件</p>
</li>
<li><p><img src="https://i.loli.net/2021/03/15/AhiPn4rz39uQTFd.png" alt="image-20210315154109338"></p>
</li>
<li><p>而里面尤为重要的是</p>
<p>port：8080是端口号，可以进行修改。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">Connector</span> <span class="attr">port</span>=<span class="string">&quot;8080&quot;</span> <span class="attr">protocol</span>=<span class="string">&quot;HTTP/1.1&quot;</span></span></span><br><span class="line"><span class="tag">              <span class="attr">connectionTimeout</span>=<span class="string">&quot;20000&quot;</span></span></span><br><span class="line"><span class="tag">              <span class="attr">redirectPort</span>=<span class="string">&quot;8443&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>host name =”localhost”是主机，可以修改成域名</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;Host <span class="attribute">name</span>=<span class="string">&quot;localhost&quot;</span>  <span class="attribute">appBase</span>=<span class="string">&quot;webapps&quot;</span></span><br><span class="line">            <span class="attribute">unpackWARs</span>=<span class="string">&quot;true&quot;</span> <span class="attribute">autoDeploy</span>=<span class="string">&quot;true&quot;</span>&gt;</span><br></pre></td></tr></table></figure>

<p>那么如何更改呢？如果只是把<code>localhost</code>改成<code>www.liefox.com</code>的话我们是访问不到tomcat的页面，我们要改c盘某个角落的host文件</p>
<p>文件路径：C:\Windows\System32\drivers\etc</p>
<p><img src="https://i.loli.net/2021/03/15/gEyD7cKFizdVfk3.png" alt="image-20210315155819492"></p>
<p>将127.0.0.1映射到<a target="_blank" rel="noopener" href="http://www.liefox.com/">www.liefox.com</a></p>
<p><img src="https://i.loli.net/2021/03/15/kLYz1XcHMBmotSd.png" alt="image-20210315160345530"></p>
<p>成功映射到<a target="_blank" rel="noopener" href="http://www.liefox.com记得加端号8080/">www.liefox.com记得加端号8080</a></p>
</li>
</ul>
<p><img src="https://i.loli.net/2021/03/15/PUOh8txKnjFJYvi.png" alt="image-20210315160216472"></p>
</blockquote>
<blockquote>
<ul>
<li><h4 id="webapps文件夹"><a href="#webapps文件夹" class="headerlink" title="webapps文件夹"></a>webapps文件夹</h4></li>
<li><p>是放web应用的文件夹（我们来试验一下）</p>
</li>
<li><p><img src="https://i.loli.net/2021/03/15/ml6dEZ3zycY9XG8.png" alt="image-20210315190500643"></p>
</li>
<li><p>然后打开就是默认的tomcat网页，我们留下WEB-INF文件夹，将其他的都删掉，然后自己写个html网页</p>
</li>
</ul>
<p><img src="https://i.loli.net/2021/03/15/wBxCJuOqQTL4Kv5.png" alt="image-20210315190837432"></p>
<ul>
<li>重新部署一下，访问localhost：8080</li>
<li><img src="https://i.loli.net/2021/03/15/Syu61njEZbftDOs.png" alt="image-20210315190935932"></li>
</ul>
</blockquote>
<h2 id="HTTP"><a href="#HTTP" class="headerlink" title="HTTP"></a>HTTP</h2><h3 id="HTTP概念"><a href="#HTTP概念" class="headerlink" title="HTTP概念"></a>HTTP概念</h3><blockquote>
<p>HTTP：超文本传输协议</p>
<p>超文本：音乐、视频、图片、定位、地图。。。。</p>
<p>端口：80</p>
<hr>
<p>HTTPS：安全的 -http plus</p>
<p>端口：443</p>
</blockquote>
<h3 id="HTTP版本"><a href="#HTTP版本" class="headerlink" title="HTTP版本"></a>HTTP版本</h3><blockquote>
<ul>
<li><p>http 1.0：客户端与服务器进行连接后，只可以获取一个web资源，后断开连接。</p>
</li>
<li><p>http 1.1：客户端与服务器进行连接后，可以获取多个web资源，不会断开连接。</p>
</li>
</ul>
</blockquote>
<h3 id="HTTP请求"><a href="#HTTP请求" class="headerlink" title="HTTP请求"></a>HTTP请求</h3><h4 id="request"><a href="#request" class="headerlink" title="request"></a>request</h4><blockquote>
<ul>
<li><p>浏览器请求服务器</p>
<figure class="highlight oxygene"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Request URL: https:<span class="comment">//www.baidu.com/		请求地址</span></span><br><span class="line">Request <span class="function"><span class="keyword">Method</span>:</span> GET		常有的有get、post</span><br><span class="line">Status Code: <span class="number">200</span> OK		状态码</span><br><span class="line">Remote Address: <span class="number">36.152</span>.<span class="number">44.96</span>:<span class="number">443</span>		远程地址</span><br></pre></td></tr></table></figure>

<h3 id="请求行"><a href="#请求行" class="headerlink" title="请求行"></a>请求行</h3><p>Get和Post请求方法</p>
<p>Get：不安全，会在浏览器的地址栏中显示内容，但效率高</p>
<p>post：安全，不会在浏览器的地址栏中显示内容，但效率低</p>
<h3 id="消息体"><a href="#消息体" class="headerlink" title="消息体"></a>消息体</h3><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">Accept:</span> 告诉浏览器所支持的数据类型</span><br><span class="line">Accept-<span class="attr">Encoding:</span> 支持哪种编码格式</span><br><span class="line">Accept-<span class="attr">Language:</span> 告诉浏览器语言环境</span><br><span class="line">Cache-<span class="attr">Control:</span> 缓冲控制</span><br><span class="line"><span class="attr">Connection:</span> 保持连接还是断开</span><br><span class="line"><span class="attr">Host:</span> 主机地址</span><br></pre></td></tr></table></figure>
</li>
</ul>
</blockquote>
<hr>
<h3 id="HTTP响应"><a href="#HTTP响应" class="headerlink" title="HTTP响应"></a>HTTP响应</h3><h4 id="response"><a href="#response" class="headerlink" title="response"></a>response</h4><blockquote>
<ul>
<li>服务器响应客户端</li>
</ul>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;Cache-<span class="attr">Control:</span> <span class="keyword">private</span>				   缓冲控制</span><br><span class="line">&gt;<span class="attr">Connection:</span> keep-alive				   连接</span><br><span class="line">&gt;Content-<span class="attr">Encoding:</span> gzip				   编码</span><br><span class="line">&gt;Content-<span class="attr">Type:</span> text/html;charset=utf<span class="number">-8</span>	类型</span><br></pre></td></tr></table></figure>

<ul>
<li><h3 id="响应头"><a href="#响应头" class="headerlink" title="响应头"></a>响应头</h3></li>
</ul>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&gt;<span class="attr">Accept:</span> 告诉浏览器所支持的数据类型</span><br><span class="line">&gt;Accept-<span class="attr">Encoding:</span> 支持哪种编码格式</span><br><span class="line">&gt;Accept-<span class="attr">Language:</span> 告诉浏览器语言环境</span><br><span class="line">&gt;Cache-<span class="attr">Control:</span> 缓冲控制</span><br><span class="line">&gt;<span class="attr">Connection:</span> 保持连接还是断开</span><br><span class="line">&gt;<span class="attr">Host:</span> 主机地址</span><br><span class="line">&gt;<span class="attr">Refresh:</span> 告诉客户端多久刷新一次</span><br><span class="line">&gt;<span class="attr">Location:</span> 让网页重新定位</span><br></pre></td></tr></table></figure>

<h3 id="响应状态码"><a href="#响应状态码" class="headerlink" title="响应状态码"></a>响应状态码</h3><ul>
<li>200：请求响应正常</li>
<li>3XX：请求重定向</li>
<li>4XX：找不到资源</li>
<li>5XX：服务器代码错误</li>
</ul>
</blockquote>
<h2 id="Maven"><a href="#Maven" class="headerlink" title="Maven"></a>Maven</h2><h3 id="Maven配置，以及搭建Maven项目。。。"><a href="#Maven配置，以及搭建Maven项目。。。" class="headerlink" title="Maven配置，以及搭建Maven项目。。。"></a>Maven配置，以及搭建Maven项目。。。</h3><p>请看往期文章：<a href="https://cxq21.gitee.io/posts/195cf5d1.html">Maven详讲</a></p>
<h2 id="Servlet"><a href="#Servlet" class="headerlink" title="Servlet"></a>Servlet</h2><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>Servlet是sun公司推出的一个专门编写web程序的接口，要完成一个web程序通常需要俩步：</p>
<ul>
<li>用Java实现Servlet接口</li>
<li>将web应用部署在服务器上</li>
</ul>
<p>将实现了Servlet接口的java程序称为Servlet。</p>
<h3 id="Hello-Servlet"><a href="#Hello-Servlet" class="headerlink" title="Hello Servlet"></a>Hello Servlet</h3><p>用Maven快速创建一个web项目，请看往期文章：<a href="https://cxq21.gitee.io/posts/195cf5d1.html#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAweb%E9%A1%B9%E7%9B%AE">Maven快速创建web项目</a></p>
<h4 id="编写Servlet"><a href="#编写Servlet" class="headerlink" title="编写Servlet"></a>编写Servlet</h4><p>Servlet提供了两个接口来实现web程序：HttpServlet（主）、GenericServlet</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">HelloServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doGet(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        <span class="type">PrintWriter</span> writer = resp.getWriter();<span class="comment">//响应流</span></span><br><span class="line">        writer.print(<span class="string">&quot;Hello servlet&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doPost(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        <span class="keyword">super</span>.doPost(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>配置web.xml文件（编写Servlet映射）</p>
<p><code>需要给浏览器一个访问的路径</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">web-app</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://xmlns.jcp.org/xml/ns/javaee</span></span></span><br><span class="line"><span class="tag"><span class="string">                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">version</span>=<span class="string">&quot;4.0&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">metadata-complete</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--注册servlet--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>hello<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.servlet.HelloServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--servlet的请求路径--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>hello<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/hello<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">web-app</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="配置tomcat"><a href="#配置tomcat" class="headerlink" title="配置tomcat"></a>配置tomcat</h4><p><img src="https://i.loli.net/2021/03/16/DHPytXLBv83CNSc.png" alt="image-20210316101753583"></p>
<h4 id="运行tomcat"><a href="#运行tomcat" class="headerlink" title="运行tomcat"></a>运行tomcat</h4><p><img src="https://i.loli.net/2021/03/16/SZtmyYsLMIbXEAi.png" alt="image-20210316101831813"></p>
<h4 id="访问hello"><a href="#访问hello" class="headerlink" title="访问hello"></a>访问hello</h4><p><img src="https://i.loli.net/2021/03/16/VbXNBMWOQCk4GHj.png" alt="image-20210316101909315"></p>
<h3 id="Servlet原理"><a href="#Servlet原理" class="headerlink" title="Servlet原理"></a>Servlet原理</h3><p><img src="https://i.loli.net/2021/03/16/dOYr6HaExzA74In.png"></p>
<h3 id="Mapping"><a href="#Mapping" class="headerlink" title="Mapping"></a>Mapping</h3><p>servlet和servlet-mapping都是成对出现。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--注册servlet--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>hello<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.servlet.HelloServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--servlet的请求路径--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>hello<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/hello<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="路径配置的多种情况"><a href="#路径配置的多种情况" class="headerlink" title="路径配置的多种情况"></a>路径配置的多种情况</h4><ul>
<li><p>默认</p>
<figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="built_in">url</span>-pattern&gt;<span class="regexp">/hello&lt;/u</span>rl-pattern&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>后缀（<code>注意：路径前不用 /</code>)</p>
<figure class="highlight coq"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;url-<span class="built_in">pattern</span>&gt;*.<span class="built_in">do</span>&lt;/url-<span class="built_in">pattern</span>&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>通配符（不推荐）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/*<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="编写404页面（利用通配符）"><a href="#编写404页面（利用通配符）" class="headerlink" title="编写404页面（利用通配符）"></a>编写404页面（利用通配符）</h4><h5 id="编写servlet程序"><a href="#编写servlet程序" class="headerlink" title="编写servlet程序"></a>编写servlet程序</h5><p><img src="https://i.loli.net/2021/03/16/64imo3qplcLdVHh.png" alt="image-20210316105332937"></p>
<h5 id="编写web-xml映射"><a href="#编写web-xml映射" class="headerlink" title="编写web.xml映射"></a>编写web.xml映射</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--404--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>erro<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.servlet.Erro<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>erro<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/*<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="地址栏输入任意结果，模拟404效果"><a href="#地址栏输入任意结果，模拟404效果" class="headerlink" title="地址栏输入任意结果，模拟404效果"></a>地址栏输入任意结果，模拟404效果</h5><p><code>注意：通配符优先级最高，初始页面为404</code></p>
<p><img src="https://i.loli.net/2021/03/16/sFbGv8OpdUWKf4I.png" alt="image-20210316105945027"></p>
<h2 id="ServletContext"><a href="#ServletContext" class="headerlink" title="ServletContext"></a>ServletContext</h2><p>ServletContext：应用上下文，凌驾于Servlet之上，web容器打开后，它会为每个web程序创建一个ServletContext对象，他代表当前的web应用。</p>
<h3 id="共享数据"><a href="#共享数据" class="headerlink" title="共享数据"></a>共享数据</h3><p>在不同的Servlet存储数据，并且获取。</p>
<ul>
<li><p>编写一个存储数据的Servlet类</p>
<p><img src="https://i.loli.net/2021/03/16/AzYI1HSZnrdtCKe.png" alt="image-20210316133627614"></p>
</li>
<li><p>编写一个获取数据的Servlet</p>
<p><img src="https://i.loli.net/2021/03/16/1zUPbVajMALn36d.png" alt="image-20210316134259847"></p>
</li>
<li><p>编写web路径映射</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--注册servlet--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>servlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.servlet.Servlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--servlet的请求路径--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>servlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/servlet<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--注册servlet--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>getc<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.servlet.GetContext<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--servlet的请求路径--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>getc<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/getc<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>运行tomcat</p>
<p>访问默认页面</p>
<p><img src="https://i.loli.net/2021/03/16/lH5UXtePmdj6Zhu.png" alt="image-20210316141055944"></p>
<p>直接输入/getc,是不会得到结果的，而是得到null，因为存放数据的的Servlet还有访问，数据还没有放到ServletContext上。</p>
<p><img src="https://i.loli.net/2021/03/16/K5AeOG6Vt9YNPfm.png" alt="image-20210316141227669"></p>
<p>我们先访问一下servlet</p>
<p><img src="https://i.loli.net/2021/03/16/fitwEe7sBPAgF93.png" alt="image-20210316141444131"></p>
<p>此时，数据已经放到了context里，然后在看看getc</p>
<p><img src="https://i.loli.net/2021/03/16/mBrd2VKsjpGeRDC.png" alt="image-20210316141549166"></p>
<p>成功访问到。</p>
</li>
</ul>
<h3 id="获取初始化参数"><a href="#获取初始化参数" class="headerlink" title="获取初始化参数"></a>获取初始化参数</h3><p>ServletContext还可以获取初始化参数</p>
<ul>
<li><p>在web.xml编写一个初始化参数</p>
<figure class="highlight sqf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;context-<span class="built_in">param</span>&gt;</span><br><span class="line">        &lt;<span class="built_in">param</span>-<span class="built_in">name</span>&gt;url&lt;/<span class="built_in">param</span>-<span class="built_in">name</span>&gt;</span><br><span class="line">        &lt;<span class="built_in">param</span>-value&gt;cxq21.gitee.io&lt;/<span class="built_in">param</span>-value&gt;</span><br><span class="line">&lt;/context-<span class="built_in">param</span>&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写一个获取初始化参数的servlet的类</p>
<p><img src="https://i.loli.net/2021/03/16/UoxtLXD2hKpu3Nl.png" alt="image-20210316145143895"></p>
</li>
<li><p>在web.xml编写映射路径</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--注册servlet--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>getp<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.servlet.GetParam<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--servlet的请求路径--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>getp<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/getp<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>启动tomcat</p>
</li>
<li><p><img src="https://i.loli.net/2021/03/16/xHb7QcUsktASaOX.png" alt="image-20210316145805170"></p>
</li>
</ul>
<h3 id="请求转发"><a href="#请求转发" class="headerlink" title="请求转发"></a>请求转发</h3><ul>
<li><p>编写一个请求转发的类，访问该Servlet转发到、getp的页面</p>
<p><img src="https://i.loli.net/2021/03/16/WxTUoqnRemdSJOL.png" alt="image-20210316153157156"></p>
</li>
<li><p>编写web映射路径文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--注册servlet--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>sd1<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.servlet.ServletDemo<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--servlet的请求路径--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>sd1<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/sd1<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>启动tomcat，访问路径 <code>/sd1</code>,转发成功。</p>
<p><img src="https://i.loli.net/2021/03/16/ClqLRQbkGyJIZox.png" alt="image-20210316153428434"></p>
</li>
<li><p>图解</p>
<p><img src="https://i.loli.net/2021/03/16/U8xJLrpzcg7dwNW.png" alt="image-20210316153920246"></p>
</li>
</ul>
<h3 id="读取资源文件"><a href="#读取资源文件" class="headerlink" title="读取资源文件"></a>读取资源文件</h3><p>获取一个properties文件里的内容</p>
<ul>
<li><p>创建一个properties文件</p>
<p><img src="https://i.loli.net/2021/03/16/T2pdL7kXHnEQO8i.png" alt="image-20210316155548708"></p>
</li>
<li><p>在编写一个servlet类</p>
<p><img src="https://i.loli.net/2021/03/16/wBbvrk5KihC6Lta.png" alt="image-20210316164931272"></p>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@Override</span><br><span class="line"> protected void <span class="keyword">do</span><span class="constructor">Get(HttpServletRequest <span class="params">req</span>, HttpServletResponse <span class="params">resp</span>)</span> throws ServletException, IOException &#123;</span><br><span class="line">     InputStream stream = this.get<span class="constructor">ServletContext()</span></span><br><span class="line">             .get<span class="constructor">ResourceAsStream(<span class="string">&quot;/WEB-INF/classes/db.properties&quot;</span>)</span>;<span class="comment">//获取的路径为，部署后的properties的位置</span></span><br><span class="line">     Properties properties = <span class="keyword">new</span> <span class="constructor">Properties()</span>;</span><br><span class="line">     properties.load(stream);</span><br><span class="line">     String user = properties.get<span class="constructor">Property(<span class="string">&quot;user&quot;</span>)</span>;</span><br><span class="line">     String password = properties.get<span class="constructor">Property(<span class="string">&quot;password&quot;</span>)</span>;</span><br><span class="line">     resp.get<span class="constructor">Writer()</span>.print(user+password);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>部署后的properties的位置，需要一个文件流来获取该位置</p>
<p><img src="https://i.loli.net/2021/03/16/zsEMweWumSaoBhb.png" alt="image-20210316165614260"></p>
</li>
<li><p>编写web映射路径文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--注册servlet--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>sd2<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.servlet.ServletDemo1<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">   <span class="comment">&lt;!--servlet的请求路径--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>sd2<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/sd2<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>启动tomcat后访问</p>
<p><img src="https://i.loli.net/2021/03/16/5so2GvNuAlneqD1.png" alt="image-20210316165713571"></p>
</li>
</ul>
<h2 id="HTTPServletResponse"><a href="#HTTPServletResponse" class="headerlink" title="HTTPServletResponse"></a>HTTPServletResponse</h2><p>web服务器接收到http请求，针对这个请求，分别创建一个代表请求，分别创建一个代表请求的HTTPServletRequest对象，代表响应的一个HTTPServletResponse；</p>
<h3 id="下载文件"><a href="#下载文件" class="headerlink" title="下载文件"></a>下载文件</h3><ol>
<li><p>把图片放在resource文件夹下</p>
<img src="https://i.loli.net/2021/03/16/u52qWg6QDJc3y8n.png" alt="image-20210316191858959" style="zoom: 33%;" />
</li>
<li><p>要获取下载文件的路径</p>
</li>
<li><p>下载的文件名是啥</p>
</li>
<li><p>设置想办法让浏览器能够支持下载我们需要的东西</p>
</li>
<li><p>获取下载文件的输入流</p>
</li>
<li><p>创建缓冲器</p>
</li>
<li><p>获取OutputStream对象</p>
</li>
<li><p>将FileOutputStream流写入到buffer缓冲区</p>
</li>
<li><p>使用OutputStream将缓冲区中的数据输出到客户端</p>
<p>源码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.liefox.RP;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.ServletException;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.ServletOutputStream;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServlet;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.net.URLEncoder;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/16 下午 5:41</span></span><br><span class="line"><span class="comment"> * description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RP</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        <span class="comment">//1. 要获取下载文件的路径</span></span><br><span class="line">        String RealPath = <span class="string">&quot;D:\\idea\\Java_Web\\Response\\target\\classes\\桌面.png&quot;</span>;</span><br><span class="line">        System.out.println(RealPath);</span><br><span class="line">        <span class="comment">//2. 下载的文件名是啥</span></span><br><span class="line">        String fileName = RealPath.substring(RealPath.lastIndexOf(<span class="string">&quot;\\&quot;</span>) + <span class="number">1</span>);</span><br><span class="line">        <span class="comment">//3. 设置想办法让浏览器能够支持下载我们需要的东西,中文文件名URLEncoder.encode，否则会乱码</span></span><br><span class="line">        resp.setHeader(<span class="string">&quot;Content-Disposition&quot;</span>, <span class="string">&quot;attachment;filename=&quot;</span> + URLEncoder.encode(fileName, <span class="string">&quot;UTF-8&quot;</span>));</span><br><span class="line">        <span class="comment">//4. 获取下载文件的输入流</span></span><br><span class="line">        FileInputStream inputStream = <span class="keyword">new</span> FileInputStream(RealPath);</span><br><span class="line">        <span class="comment">//5. 创建缓冲器</span></span><br><span class="line">        <span class="keyword">int</span> len=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">byte</span>[] buffer = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="comment">//6. 获取OutputStream对象</span></span><br><span class="line">        ServletOutputStream outputStream = resp.getOutputStream();</span><br><span class="line">        <span class="comment">//7. 将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区中的数据输出到客户端</span></span><br><span class="line">        <span class="keyword">while</span> ((len=inputStream.read(buffer))&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            outputStream.write(buffer,<span class="number">0</span>,len);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//8. close流</span></span><br><span class="line">        inputStream.close();</span><br><span class="line">        outputStream.close();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        doGet(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>启动服务后，输入地址（映射地址为 <code>rp</code>，下载图片</p>
<p><img src="https://i.loli.net/2021/03/16/yTpn3tcKVmlZGAW.png" alt="image-20210316192752957"></p>
</li>
</ol>
<h3 id="验证码"><a href="#验证码" class="headerlink" title="验证码"></a>验证码</h3><figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">package com.liefox.RP;</span><br><span class="line"></span><br><span class="line">import javax.imageio.ImageIO;</span><br><span class="line">import javax.servlet.ServletException;</span><br><span class="line">import javax.servlet.http.HttpServlet;</span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line">import java.awt.*;</span><br><span class="line">import java.awt.image.BufferedImage;</span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web</span></span><br><span class="line"><span class="comment"> * @author: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/16 下午 7:49</span></span><br><span class="line"><span class="comment"> * description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="keyword">class</span> ImgServlet extends HttpServlet &#123;</span><br><span class="line">    @Override</span><br><span class="line">    protected void <span class="keyword">do</span><span class="constructor">Get(HttpServletRequest <span class="params">req</span>, HttpServletResponse <span class="params">resp</span>)</span> throws ServletException, IOException &#123;</span><br><span class="line">        <span class="comment">//如何让浏览器3秒刷新一次</span></span><br><span class="line">        resp.set<span class="constructor">Header(<span class="string">&quot;refresh&quot;</span>,<span class="string">&quot;3&quot;</span>)</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//在内存中创建一个图片</span></span><br><span class="line">        BufferedImage image = <span class="keyword">new</span> <span class="constructor">BufferedImage(80,20,BufferedImage.TYPE_INT_RGB)</span>;</span><br><span class="line">        <span class="comment">//得到图片</span></span><br><span class="line">        Graphics2D g =(Graphics2D) image.get<span class="constructor">Graphics()</span>;</span><br><span class="line">        <span class="comment">//设置图片的背景的颜色</span></span><br><span class="line">        g.set<span class="constructor">Color(Color.BLUE)</span>;</span><br><span class="line">        g.fill<span class="constructor">Rect(0,0,80,20)</span>;</span><br><span class="line">        <span class="comment">//给图片写数据</span></span><br><span class="line">        g.set<span class="constructor">Color(Color.<span class="params">white</span>)</span>;</span><br><span class="line">        g.set<span class="constructor">Font(<span class="params">new</span> Font(<span class="params">null</span>,Font.BOLD,20)</span>);</span><br><span class="line">        g.draw<span class="constructor">String(<span class="params">makeNum</span>()</span>,<span class="number">0</span>,<span class="number">20</span>);</span><br><span class="line">        <span class="comment">//告诉浏览器，这个请求用图片的方式打开</span></span><br><span class="line">        resp.set<span class="constructor">ContentType(<span class="string">&quot;image/jpeg&quot;</span>)</span>;</span><br><span class="line">        <span class="comment">//网站存在缓冲，不让浏览器缓存</span></span><br><span class="line">        resp.set<span class="constructor">DateHeader(<span class="string">&quot;expires&quot;</span>,-1)</span>;</span><br><span class="line">        resp.set<span class="constructor">Header(<span class="string">&quot;Cache-Control&quot;</span>,<span class="string">&quot;no-cache&quot;</span>)</span>;</span><br><span class="line">        resp.set<span class="constructor">Header(<span class="string">&quot;Pragma&quot;</span>,<span class="string">&quot;no-cache&quot;</span>)</span>;</span><br><span class="line">        <span class="comment">//把图片写给浏览器</span></span><br><span class="line">        <span class="module-access"><span class="module"><span class="identifier">ImageIO</span>.</span></span>write(image,<span class="string">&quot;jpg&quot;</span>,resp.get<span class="constructor">OutputStream()</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//生成随机数</span></span><br><span class="line">    <span class="keyword">private</span> String make<span class="constructor">Num()</span> &#123;</span><br><span class="line">        Random random = <span class="keyword">new</span> <span class="constructor">Random()</span>;</span><br><span class="line">        String num = random.next<span class="constructor">Int(9999999)</span> + <span class="string">&quot;&quot;</span>;</span><br><span class="line">        StringBuffer stringBuffer = <span class="keyword">new</span> <span class="constructor">StringBuffer()</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i=<span class="number">0</span>;i&lt;<span class="number">7</span>- num.length<span class="literal">()</span>;i++)&#123;</span><br><span class="line">            stringBuffer.append(<span class="string">&quot;0&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        num=stringBuffer.<span class="keyword">to</span><span class="constructor">String()</span>+num;</span><br><span class="line">        return num;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    protected void <span class="keyword">do</span><span class="constructor">Post(HttpServletRequest <span class="params">req</span>, HttpServletResponse <span class="params">resp</span>)</span> throws ServletException, IOException &#123;</span><br><span class="line">        <span class="keyword">do</span><span class="constructor">Get(<span class="params">req</span>, <span class="params">resp</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>编写映射路径，启动服务，访问，</p>
<p><img src="https://i.loli.net/2021/03/17/kSvzEPhVMT67iBd.png" alt="image-20210317091757211"></p>
<h3 id="重定向"><a href="#重定向" class="headerlink" title="重定向"></a>重定向</h3><figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">protected void <span class="keyword">do</span><span class="constructor">Get(HttpServletRequest <span class="params">req</span>, HttpServletResponse <span class="params">resp</span>)</span> throws ServletException, IOException &#123;</span><br><span class="line">        resp.send<span class="constructor">Redirect(<span class="string">&quot;/Resp/img&quot;</span>)</span>;<span class="comment">//重定向</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="重定向于转发区别"><a href="#重定向于转发区别" class="headerlink" title="重定向于转发区别"></a>重定向于转发区别</h4><ul>
<li><p>重定向URL地址会发生变化 <code>302</code></p>
</li>
<li><p>转发url地址不会 <code>307</code></p>
</li>
<li><p>图解</p>
<p><img src="https://i.loli.net/2021/03/16/U8xJLrpzcg7dwNW.png" alt="image-20210316153920246"></p>
</li>
</ul>
<h2 id="HTTPServletRequest"><a href="#HTTPServletRequest" class="headerlink" title="HTTPServletRequest"></a>HTTPServletRequest</h2><h3 id="获取前端表单数据-1"><a href="#获取前端表单数据-1" class="headerlink" title="获取前端表单数据-1"></a>获取前端表单数据-1</h3><ul>
<li><p>获取 jsp Maven</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>javax.servlet.jsp<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jsp-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1-rev-1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">scope</span>&gt;</span>provided<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>编写Requset</p>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">public <span class="keyword">class</span> RequestTest extends HttpServlet &#123;</span><br><span class="line">    @Override</span><br><span class="line">    protected void <span class="keyword">do</span><span class="constructor">Get(HttpServletRequest <span class="params">req</span>, HttpServletResponse <span class="params">resp</span>)</span> throws ServletException, IOException &#123;</span><br><span class="line">        <span class="module-access"><span class="module"><span class="identifier">System</span>.</span></span>out.println(<span class="string">&quot;进入了这个请求了&quot;</span>);</span><br><span class="line">        <span class="comment">//处理请求</span></span><br><span class="line">        String user = req.get<span class="constructor">Parameter(<span class="string">&quot;user&quot;</span>)</span>;</span><br><span class="line">        String password = req.get<span class="constructor">Parameter(<span class="string">&quot;password&quot;</span>)</span>;</span><br><span class="line">        <span class="module-access"><span class="module"><span class="identifier">System</span>.</span></span>out.println(user + <span class="string">&quot;:&quot;</span> + password);</span><br><span class="line">        <span class="comment">//请求转发,注意路径不可写错，否则会404</span></span><br><span class="line">        resp.send<span class="constructor">Redirect(<span class="string">&quot;/Resp/Succes.jsp &quot;</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    protected void <span class="keyword">do</span><span class="constructor">Post(HttpServletRequest <span class="params">req</span>, HttpServletResponse <span class="params">resp</span>)</span> throws ServletException, IOException &#123;</span><br><span class="line">        <span class="keyword">do</span><span class="constructor">Get(<span class="params">req</span>, <span class="params">resp</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写表单</p>
<figure class="highlight erb"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">&lt;%</span><span class="ruby">@ page language=<span class="string">&quot;java&quot;</span> contentType=<span class="string">&quot;text/html; charset=utf-8&quot;</span> </span><span class="xml">%&gt;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>Hello World!<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="xml">&lt;%-</span><span class="ruby">-这里提交的路径，需要寻找到项目的路径-</span><span class="xml">-%&gt;</span></span><br><span class="line"><span class="xml">&lt;%-</span><span class="ruby">-<span class="variable">$&#123;</span>pageContext.request.contextPath&#125;代表当前项目-</span><span class="xml">-%&gt;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;$&#123;pageContext.request.contextPath&#125;/request&quot;</span> <span class="attr">method</span>=<span class="string">&quot;get&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">    用户名：<span class="tag">&lt;<span class="name">input</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span></span><br><span class="line"><span class="xml">    密码：<span class="tag">&lt;<span class="name">input</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">type</span>=<span class="string">&quot;password&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>编写成功页面</p>
<figure class="highlight erb"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">&lt;%</span><span class="ruby">@ page contentType=<span class="string">&quot;text/html;charset=UTF-8&quot;</span> language=<span class="string">&quot;java&quot;</span> </span><span class="xml">%&gt;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>Succes<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>编写映射路径，并启动访问</p>
<p>输入用户名和密码，点击提交</p>
<p><img src="https://i.loli.net/2021/03/17/F5GOR8kfvBLEcWl.png" alt="image-20210317101602176"></p>
</li>
<li><p>重定向到Success页面</p>
<p><img src="https://i.loli.net/2021/03/17/Y6cCPpVEQiF2HDg.png" alt="image-20210317101705851"></p>
</li>
<li><p>后台收到数据，并控制台打印数据</p>
<p><img src="https://i.loli.net/2021/03/17/S5jVgpLqhGw9CPo.png" alt="image-20210317101900949"></p>
</li>
</ul>
<h3 id="获取前端表单数据-2"><a href="#获取前端表单数据-2" class="headerlink" title="获取前端表单数据-2"></a>获取前端表单数据-2</h3><ul>
<li><h4 id="编写request类"><a href="#编写request类" class="headerlink" title="编写request类"></a>编写request类</h4><figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">package com.liefox.request;</span><br><span class="line"></span><br><span class="line">import javax.servlet.ServletContext;</span><br><span class="line">import javax.servlet.ServletException;</span><br><span class="line">import javax.servlet.http.HttpServlet;</span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web</span></span><br><span class="line"><span class="comment"> * @author: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/17 下午 12:23</span></span><br><span class="line"><span class="comment"> * description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="keyword">class</span> requestDemo extends HttpServlet &#123;</span><br><span class="line">    @Override</span><br><span class="line">    protected void <span class="keyword">do</span><span class="constructor">Get(HttpServletRequest <span class="params">req</span>, HttpServletResponse <span class="params">resp</span>)</span> throws ServletException, IOException &#123;</span><br><span class="line">        <span class="comment">//处理输入输出乱码</span></span><br><span class="line">        req.set<span class="constructor">CharacterEncoding(<span class="string">&quot;utf-8&quot;</span>)</span>;</span><br><span class="line">        resp.set<span class="constructor">CharacterEncoding(<span class="string">&quot;utf-8&quot;</span>)</span>;</span><br><span class="line">        <span class="comment">//获取前端数据</span></span><br><span class="line">        <span class="module-access"><span class="module"><span class="identifier">System</span>.</span></span>out.println(<span class="string">&quot;comming&quot;</span>);</span><br><span class="line">        String username = req.get<span class="constructor">Parameter(<span class="string">&quot;username&quot;</span>)</span>;</span><br><span class="line">        String password = req.get<span class="constructor">Parameter(<span class="string">&quot;password&quot;</span>)</span>;</span><br><span class="line">        <span class="comment">//获取数组数据</span></span><br><span class="line">        String<span class="literal">[]</span> hobbys = req.get<span class="constructor">ParameterValues(<span class="string">&quot;hobbys&quot;</span>)</span>;</span><br><span class="line">        <span class="module-access"><span class="module"><span class="identifier">System</span>.</span></span>out.println(<span class="string">&quot;--------------&quot;</span>);</span><br><span class="line">        <span class="module-access"><span class="module"><span class="identifier">System</span>.</span></span>out.println(<span class="string">&quot;用户名：&quot;</span>+username);</span><br><span class="line">        <span class="module-access"><span class="module"><span class="identifier">System</span>.</span></span>out.println(<span class="string">&quot;密码：&quot;</span>+password);</span><br><span class="line">        <span class="module-access"><span class="module"><span class="identifier">System</span>.</span></span>out.println(<span class="string">&quot;hobbys：&quot;</span>+<span class="module-access"><span class="module"><span class="identifier">Arrays</span>.</span></span><span class="keyword">to</span><span class="constructor">String(<span class="params">hobbys</span>)</span>);</span><br><span class="line">        <span class="comment">//转发success页面</span></span><br><span class="line">        ServletContext context = this.get<span class="constructor">ServletContext()</span>;</span><br><span class="line">        context.get<span class="constructor">RequestDispatcher(<span class="string">&quot;/succes.jsp&quot;</span>)</span>.forward(req,resp);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    protected void <span class="keyword">do</span><span class="constructor">Post(HttpServletRequest <span class="params">req</span>, HttpServletResponse <span class="params">resp</span>)</span> throws ServletException, IOException &#123;</span><br><span class="line">        <span class="keyword">do</span><span class="constructor">Get(<span class="params">req</span>, <span class="params">resp</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写前端页面</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; <span class="keyword">language</span>=&quot;java&quot; %&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;title&gt;Title&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;form action=&quot;$&#123;pageContext.request.contextPath&#125;\login&quot; <span class="keyword">method</span>=&quot;post&quot;&gt;</span><br><span class="line">    用户名：&lt;<span class="keyword">input</span> <span class="keyword">type</span>=&quot;text&quot; <span class="type">name</span>=&quot;username&quot;&gt;&lt;br&gt;</span><br><span class="line">    密码：&lt;<span class="keyword">input</span> <span class="keyword">type</span>=&quot;password&quot; <span class="type">name</span>=&quot;password&quot;&gt;&lt;br&gt;</span><br><span class="line">    &lt;<span class="keyword">input</span> <span class="keyword">type</span>=&quot;checkbox&quot; <span class="type">name</span>=&quot;hobbys&quot; <span class="keyword">value</span>=&quot;java&quot;&gt;java</span><br><span class="line">    &lt;<span class="keyword">input</span> <span class="keyword">type</span>=&quot;checkbox&quot; <span class="type">name</span>=&quot;hobbys&quot; <span class="keyword">value</span>=&quot;html&quot;&gt;html</span><br><span class="line">    &lt;<span class="keyword">input</span> <span class="keyword">type</span>=&quot;checkbox&quot; <span class="type">name</span>=&quot;hobbys&quot; <span class="keyword">value</span>=&quot;js&quot;&gt;js</span><br><span class="line">    &lt;<span class="keyword">input</span> <span class="keyword">type</span>=&quot;checkbox&quot; <span class="type">name</span>=&quot;hobbys&quot; <span class="keyword">value</span>=&quot;css&quot;&gt;css&lt;br&gt;</span><br><span class="line">    &lt;<span class="keyword">input</span> <span class="keyword">type</span>=&quot;submit&quot;&gt;</span><br><span class="line">&lt;/form&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight erb"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">&lt;%</span><span class="ruby">@ page contentType=<span class="string">&quot;text/html;charset=UTF-8&quot;</span> language=<span class="string">&quot;java&quot;</span> </span><span class="xml">%&gt;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>succes<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>编写映射路径文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--注册servlet--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>requestDemo<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.request.requestDemo<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--servlet的请求路径--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>requestDemo<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/login<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>启动并访问，输入信息，并提交</p>
<p><img src="https://i.loli.net/2021/03/17/8YtwdTvnkP3L5Xx.png" alt="image-20210317135601759"></p>
</li>
</ul>
<p><img src="https://i.loli.net/2021/03/17/xuB2odWOa4P1Kbj.png" alt="image-20210317135639579"></p>
<ul>
<li><p>后端控制台打印数据</p>
<p><img src="https://i.loli.net/2021/03/17/q1tSCJ7U4EwxeHX.png" alt="image-20210317135744756"></p>
</li>
</ul>
<h2 id="Cookie、Session"><a href="#Cookie、Session" class="headerlink" title="Cookie、Session"></a>Cookie、Session</h2><h3 id="会话"><a href="#会话" class="headerlink" title="会话"></a>会话</h3><p>用户打开一个浏览器，点击了很多超链接（网页），访问了多个web资源，到关闭浏览器，称为会话。</p>
<h4 id="有状态会话"><a href="#有状态会话" class="headerlink" title="有状态会话"></a>有状态会话</h4><p>如何证明你来过这个网站？</p>
<ul>
<li>当你访问web资源的时候，会给你一个Cookie；</li>
<li>访问web资源后，服务器上会登记Session。</li>
</ul>
<h3 id="保存会话的两种技术"><a href="#保存会话的两种技术" class="headerlink" title="保存会话的两种技术"></a>保存会话的两种技术</h3><h4 id="Cookie"><a href="#Cookie" class="headerlink" title="Cookie"></a>Cookie</h4><p>客户端技术（请求、响应）</p>
<h4 id="Session"><a href="#Session" class="headerlink" title="Session"></a>Session</h4><p>服务器技术，可以保存用户的会话信息，可以把数据和信息存入Session。</p>
<h4 id="比如：登录网站后，下次就不用再登录了。"><a href="#比如：登录网站后，下次就不用再登录了。" class="headerlink" title="比如：登录网站后，下次就不用再登录了。"></a>比如：登录网站后，下次就不用再登录了。</h4><ul>
<li><p>编写类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.liefox.cookie;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.ServletException;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.Cookie;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServlet;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.PrintWriter;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web-02</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/17 下午 4:12</span></span><br><span class="line"><span class="comment"> * description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CookieDemo1</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        req.setCharacterEncoding(<span class="string">&quot;utf-8&quot;</span>);</span><br><span class="line">        resp.setCharacterEncoding(<span class="string">&quot;utf-8&quot;</span>);</span><br><span class="line">        resp.setContentType(<span class="string">&quot;text/html; charset=UTF-8&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//cookie,服务端从客户端获取</span></span><br><span class="line">        Cookie[] cookies = req.getCookies();<span class="comment">//这里返回数组，返回Cookie可能存在多个</span></span><br><span class="line">        PrintWriter out = resp.getWriter();</span><br><span class="line">        <span class="comment">//判断Cookie是否存在</span></span><br><span class="line">        <span class="keyword">if</span> (cookies!=<span class="keyword">null</span>)&#123;</span><br><span class="line">            out.write(<span class="string">&quot;上次访问时间：&quot;</span>);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; cookies.length; i++) &#123;</span><br><span class="line">                Cookie cookie = cookies[i];</span><br><span class="line">                <span class="comment">//获取Cookie的名字</span></span><br><span class="line">                <span class="keyword">if</span> (cookie.getName().equals(<span class="string">&quot;lastLoginTime&quot;</span>)) &#123;</span><br><span class="line">                    <span class="comment">//获取Cookie的值</span></span><br><span class="line">                    <span class="keyword">long</span> l = Long.parseLong(cookie.getValue());</span><br><span class="line">                    Date date = <span class="keyword">new</span> Date(l);</span><br><span class="line">                    out.write(date.toLocaleString());</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            out.write(<span class="string">&quot;第一次访问本站&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//服务给客户端响应一个Cookie</span></span><br><span class="line">        Cookie cookie = <span class="keyword">new</span> Cookie(<span class="string">&quot;lastLoginTime&quot;</span>, System.currentTimeMillis()+<span class="string">&quot;&quot;</span>);</span><br><span class="line">        resp.addCookie(cookie);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        doGet(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src="https://i.loli.net/2021/03/17/SXofuRmpbkBEF1W.png" alt="image-20210317170247740"></p>
<ul>
<li>第二次打开</li>
</ul>
</li>
</ul>
<p><img src="https://i.loli.net/2021/03/17/FTpolU8WiD4b2Re.png" alt="image-20210317170336217"></p>
<ul>
<li><p>Cookie图解</p>
<p><img src="https://i.loli.net/2021/03/17/RlLYfsy7zoITBcO.png" alt="image-20210317195338456"></p>
</li>
</ul>
<h3 id="Session（重点）"><a href="#Session（重点）" class="headerlink" title="Session（重点）"></a>Session（重点）</h3><h4 id="什么是Session？"><a href="#什么是Session？" class="headerlink" title="什么是Session？"></a>什么是Session？</h4><ul>
<li>服务器会给用户创建一个Session对象</li>
<li>一个session独占一个浏览器，如果关闭了，就不存在了</li>
</ul>
<h4 id="实现一个小案例"><a href="#实现一个小案例" class="headerlink" title="实现一个小案例"></a>实现一个小案例</h4><ul>
<li><p>编写Session类</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.liefox.session;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.liefox.pojo.<span class="type">Person</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.<span class="type">ServletException</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServlet</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServletRequest</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServletResponse</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpSession</span>;</span><br><span class="line"><span class="keyword">import</span> java.io.<span class="type">IOException</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web-02</span></span><br><span class="line"><span class="comment"> * @author: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/17 下午 5:21</span></span><br><span class="line"><span class="comment"> * description:存取数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">SessionDemo1</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doGet(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        <span class="comment">//解决中文乱码</span></span><br><span class="line">        req.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        resp.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        resp.setContentType(<span class="string">&quot;text/html; charset=UTF-8&quot;</span>);</span><br><span class="line">        <span class="comment">//获取session</span></span><br><span class="line">        <span class="type">HttpSession</span> session = req.getSession();</span><br><span class="line">        <span class="comment">//在session中存取数据</span></span><br><span class="line">        session.setAttribute(<span class="string">&quot;name&quot;</span>,<span class="keyword">new</span> <span class="type">Person</span>(<span class="number">10</span>,<span class="string">&quot;liefox&quot;</span>));</span><br><span class="line">        <span class="comment">//获取session Id</span></span><br><span class="line">        <span class="type">String</span> sessionId = session.getId();</span><br><span class="line">        <span class="comment">//判断session是不是新创建的</span></span><br><span class="line">        <span class="keyword">if</span> (session.isNew()) &#123;</span><br><span class="line">            resp.getWriter().write(<span class="string">&quot;session创建成功，ID：&quot;</span>+sessionId);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            resp.getWriter().write(<span class="string">&quot;session已存在，ID：&quot;</span>+sessionId);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doPost(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        doGet(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写Session2</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.liefox.session;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.liefox.pojo.<span class="type">Person</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.<span class="type">ServletException</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServlet</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServletRequest</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServletResponse</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpSession</span>;</span><br><span class="line"><span class="keyword">import</span> java.io.<span class="type">IOException</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web-02</span></span><br><span class="line"><span class="comment"> * @author: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/17 下午 7:21</span></span><br><span class="line"><span class="comment"> * description:获取数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">SessionDemo2</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doGet(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        <span class="comment">//解决中文乱码</span></span><br><span class="line">        req.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        resp.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        resp.setContentType(<span class="string">&quot;text/html; charset=UTF-8&quot;</span>);</span><br><span class="line">        <span class="comment">//得到session</span></span><br><span class="line">        <span class="type">HttpSession</span> session = req.getSession();</span><br><span class="line">        <span class="type">Person</span> person=(<span class="type">Person</span>)session.getAttribute(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">        <span class="type">System</span>.out.println(person.toString());</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doPost(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        doGet(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写Session3</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.liefox.session;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.<span class="type">ServletException</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServlet</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServletRequest</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpServletResponse</span>;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.<span class="type">HttpSession</span>;</span><br><span class="line"><span class="keyword">import</span> java.io.<span class="type">IOException</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web-02</span></span><br><span class="line"><span class="comment"> * @author: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/17 下午 7:37</span></span><br><span class="line"><span class="comment"> * description:清除session</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">SessionDemo3</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doGet(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        <span class="type">HttpSession</span> session = req.getSession();</span><br><span class="line">        session.removeAttribute(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">        <span class="comment">//手动注销Session</span></span><br><span class="line">        session.invalidate();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doPost(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        doGet(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写一个pojo类</p>
<img src="https://i.loli.net/2021/03/17/7ZVrFfasnDbYGtN.png" alt="image-20210317194915686" style="zoom: 50%;" />
</li>
<li><p>编写映射路径</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>SessionDemo1<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.session.SessionDemo1<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>SessionDemo1<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/s1<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>SessionDemo2<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.session.SessionDemo2<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>SessionDemo2<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/s2<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>SessionDemo3<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.liefox.session.SessionDemo3<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>SessionDemo3<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/s3<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>自动注销session配置文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--自动注销session--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">session-config</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--15分钟自动注销session--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">session-timeout</span>&gt;</span>15<span class="tag">&lt;/<span class="name">session-timeout</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">session-config</span>&gt;</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="Session图解"><a href="#Session图解" class="headerlink" title="Session图解"></a>Session图解</h4><p><img src="https://i.loli.net/2021/03/17/wnsaMy4ImpFcYhb.png" alt="image-20210317195510756"></p>
<h2 id="JSP"><a href="#JSP" class="headerlink" title="JSP"></a>JSP</h2><p>我们从前面的章节可以看到，Servlet就是一个能处理HTTP请求，发送HTTP响应的小程序，而发送响应无非就是获取<code>PrintWriter</code>，然后输出HTML：</p>
<figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">PrintWriter pw = resp.getWriter();</span><br><span class="line">pw.<span class="built_in">write</span>(<span class="string">&quot;&lt;html&gt;&quot;</span>);</span><br><span class="line">pw.<span class="built_in">write</span>(<span class="string">&quot;&lt;body&gt;&quot;</span>);</span><br><span class="line">pw.<span class="built_in">write</span>(<span class="string">&quot;&lt;h1&gt;Welcome, &quot;</span> + <span class="built_in">name</span> + <span class="string">&quot;!&lt;/h1&gt;&quot;</span>);</span><br><span class="line">pw.<span class="built_in">write</span>(<span class="string">&quot;&lt;/body&gt;&quot;</span>);</span><br><span class="line">pw.<span class="built_in">write</span>(<span class="string">&quot;&lt;/html&gt;&quot;</span>);</span><br><span class="line">pw.flush();</span><br></pre></td></tr></table></figure>

<p>只不过，用PrintWriter输出HTML比较痛苦，因为不但要正确编写HTML，还需要插入各种变量。如果想在Servlet中输出一个类似新浪首页的HTML，写对HTML基本上不太可能。</p>
<p>那么就诞生了JSP！</p>
<h3 id="JSP基础"><a href="#JSP基础" class="headerlink" title="JSP基础"></a>JSP基础</h3><p>JSP是Java Server Pages的缩写，它的文件必须放到<code>/src/main/webapp</code>下，文件名必须以<code>.jsp</code>结尾，整个文件与HTML并无太大区别，但需要插入变量，或者动态输出的地方，使用特殊指令<code>&lt;% ... %&gt;</code>。</p>
<p>我们来编写一个<code>hello.jsp</code>，内容如下：</p>
<figure class="highlight bnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">&lt;html&gt;</span></span><br><span class="line"><span class="attribute">&lt;head&gt;</span></span><br><span class="line">    <span class="attribute">&lt;title&gt;</span>Hello World - JSP<span class="attribute">&lt;/title&gt;</span></span><br><span class="line"><span class="attribute">&lt;/head&gt;</span></span><br><span class="line"><span class="attribute">&lt;body&gt;</span></span><br><span class="line">    <span class="attribute">&lt;%-- JSP Comment --%&gt;</span></span><br><span class="line">    <span class="attribute">&lt;h1&gt;</span>Hello World!<span class="attribute">&lt;/h1&gt;</span></span><br><span class="line">    <span class="attribute">&lt;p&gt;</span></span><br><span class="line">    <span class="attribute">&lt;%</span></span><br><span class="line"><span class="attribute">         out.println(&quot;Your IP address is &quot;);</span></span><br><span class="line"><span class="attribute">    %&gt;</span></span><br><span class="line">    <span class="attribute">&lt;span style=&quot;color:red&quot;&gt;</span></span><br><span class="line">        <span class="attribute">&lt;%= request.getRemoteAddr() %&gt;</span></span><br><span class="line">    <span class="attribute">&lt;/span&gt;</span></span><br><span class="line">    <span class="attribute">&lt;/p&gt;</span></span><br><span class="line"><span class="attribute">&lt;/body&gt;</span></span><br><span class="line"><span class="attribute">&lt;/html&gt;</span></span><br></pre></td></tr></table></figure>

<p>整个JSP的内容实际上是一个HTML，但是稍有不同：</p>
<ul>
<li>包含在<code>&lt;%--</code>和<code>--%&gt;</code>之间的是JSP的注释，它们会被完全忽略；</li>
<li>包含在<code>&lt;%</code>和<code>%&gt;</code>之间的是Java代码，可以编写任意Java代码；</li>
<li>如果使用<code>&lt;%= xxx %&gt;</code>则可以快捷输出一个变量的值。</li>
</ul>
<p>JSP页面内置了几个变量：</p>
<ul>
<li>out：表示HttpServletResponse的PrintWriter；</li>
<li>session：表示当前HttpSession对象；</li>
<li>request：表示HttpServletRequest对象。</li>
</ul>
<p>这几个变量可以直接使用。</p>
<p>访问JSP页面时，直接指定完整路径。例如，<code>http://localhost:8080/hello.jsp</code>，浏览器显示如下：</p>
<p><img src="https://i.loli.net/2021/03/17/AuHEfplPjSq7m1I.png" alt="image-20210317211130714"></p>
<p>JSP和Servlet有什么区别？其实它们没有任何区别，因为JSP在执行前首先被编译成一个Servlet。在Tomcat的临时目录下，可以找到一个<code>hello_jsp.java</code>的源文件，这个文件就是Tomcat把JSP自动转换成的Servlet源码：</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.apache.jsp;</span><br><span class="line"><span class="keyword">import</span> ...</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">class</span> hello_jsp <span class="keyword">extends</span> org.apache.jasper.<span class="keyword">runtime</span>.HttpJspBase</span><br><span class="line">    <span class="keyword">implements</span> org.apache.jasper.<span class="keyword">runtime</span>.JspSourceDependent,</span><br><span class="line">               org.apache.jasper.<span class="keyword">runtime</span>.JspSourceImports &#123;</span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> _jspService(<span class="keyword">final</span> javax.servlet.http.HttpServletRequest request, <span class="keyword">final</span> javax.servlet.http.HttpServletResponse response)</span><br><span class="line">        <span class="keyword">throws</span> java.io.IOException, javax.servlet.ServletException &#123;</span><br><span class="line">        ...</span><br><span class="line">        out.<span class="keyword">write</span>(<span class="string">&quot;&lt;html&gt;\n&quot;</span>);</span><br><span class="line">        out.<span class="keyword">write</span>(<span class="string">&quot;&lt;head&gt;\n&quot;</span>);</span><br><span class="line">        out.<span class="keyword">write</span>(<span class="string">&quot;    &lt;title&gt;Hello World - JSP&lt;/title&gt;\n&quot;</span>);</span><br><span class="line">        out.<span class="keyword">write</span>(<span class="string">&quot;&lt;/head&gt;\n&quot;</span>);</span><br><span class="line">        out.<span class="keyword">write</span>(<span class="string">&quot;&lt;body&gt;\n&quot;</span>);</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可见JSP本质上就是一个Servlet，只不过无需配置映射路径，Web Server会根据路径查找对应的<code>.jsp</code>文件，如果找到了，就自动编译成Servlet再执行。在服务器运行过程中，如果修改了JSP的内容，那么服务器会自动重新编译。</p>
<h3 id="JSP高级"><a href="#JSP高级" class="headerlink" title="JSP高级"></a>JSP高级</h3><p>JSP的指令非常复杂，除了<code>&lt;% ... %&gt;</code>外，JSP页面本身可以通过<code>page</code>指令引入Java类：</p>
<figure class="highlight xl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ <span class="built_in">page</span> <span class="keyword">import</span>=<span class="string">&quot;java.io.*&quot;</span> %&gt;</span><br><span class="line">&lt;%@ <span class="built_in">page</span> <span class="keyword">import</span>=<span class="string">&quot;java.util.*&quot;</span> %&gt;</span><br></pre></td></tr></table></figure>

<p>这样后续的Java代码才能引用简单类名而不是完整类名。</p>
<p>使用<code>include</code>指令可以引入另一个JSP文件：</p>
<figure class="highlight erb"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml">    &lt;%</span><span class="ruby">@ <span class="keyword">include</span> file=<span class="string">&quot;header.jsp&quot;</span></span><span class="xml">%&gt;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Index Page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">    &lt;%</span><span class="ruby">@ <span class="keyword">include</span> file=<span class="string">&quot;footer.jsp&quot;</span></span><span class="xml">%&gt;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<h3 id="JSP-Tag"><a href="#JSP-Tag" class="headerlink" title="JSP Tag"></a>JSP Tag</h3><p>JSP还允许自定义输出的tag，例如：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;c:<span class="keyword">out</span> value = <span class="string">&quot;<span class="subst">$&#123;sessionScope.user.name&#125;</span>&quot;</span>/&gt;</span><br></pre></td></tr></table></figure>

<p>JSP Tag需要正确引入taglib的jar包，并且还需要正确声明，使用起来非常复杂，对于页面开发来说，<em>不推荐</em>使用JSP Tag，因为我们后续会介绍更简单的模板引擎，这里我们不再介绍如何使用taglib。</p>
<ul>
<li>JSP是一种在HTML中嵌入动态输出的文件，它和Servlet正好相反，Servlet是在Java代码中嵌入输出HTML；</li>
<li>JSP可以引入并使用JSP Tag，但由于其语法复杂，不推荐使用；</li>
<li>JSP本身目前已经很少使用，我们只需要了解其基本用法即可。</li>
</ul>
<h2 id="JavaBean"><a href="#JavaBean" class="headerlink" title="JavaBean"></a>JavaBean</h2><p>JavaBean就是一个映射数据库表的实体类。常规写法是：</p>
<ul>
<li>拥有私有化属性</li>
<li>有构造器</li>
<li>有 get/set方法</li>
</ul>
<h3 id="栗子："><a href="#栗子：" class="headerlink" title="栗子："></a>栗子：</h3><ul>
<li><p>表</p>
<table>
<thead>
<tr>
<th align="center">id</th>
<th align="center">name</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">liefox</td>
</tr>
</tbody></table>
</li>
<li><p>JavaBean类（实体类）</p>
</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">package com.liefox.pojo;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web-02</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/17 下午 7:27</span></span><br><span class="line"><span class="comment"> * description:实体类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> int id;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">String</span> name;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="title">Person</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="title">Person</span>(<span class="params">int id, <span class="built_in">String</span> name</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.id = id;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">String</span> <span class="function"><span class="title">toString</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Person&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;id=&quot;</span> + id +</span><br><span class="line">                <span class="string">&quot;, name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> int <span class="function"><span class="title">getId</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">void</span> <span class="function"><span class="title">setId</span>(<span class="params">int id</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.id = id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">String</span> <span class="function"><span class="title">getName</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">void</span> <span class="function"><span class="title">setName</span>(<span class="params"><span class="built_in">String</span> name</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="三层架构（MVC）"><a href="#三层架构（MVC）" class="headerlink" title="三层架构（MVC）"></a>三层架构（MVC）</h2><h3 id="MVC："><a href="#MVC：" class="headerlink" title="MVC："></a>MVC：</h3><ul>
<li><p>M：model  模型</p>
</li>
<li><p>V：view     视图</p>
</li>
<li><p>C：controller    控制器</p>
</li>
<li><p>模型图解</p>
<p><img src="https://i.loli.net/2021/03/18/VO4zf7C9ctbmyjG.png" alt="image-20210318125015530"></p>
</li>
</ul>
<h2 id="Filter"><a href="#Filter" class="headerlink" title="Filter"></a>Filter</h2><p><img src="https://i.loli.net/2021/03/18/oVwtAnKm2DGL6Eg.png" alt="image-20210318130343860"></p>
<p>过滤器的最常见的应用有：</p>
<h3 id="中文乱码的过滤器。"><a href="#中文乱码的过滤器。" class="headerlink" title="中文乱码的过滤器。"></a>中文乱码的过滤器。</h3><ul>
<li><p>Maven依赖</p>
<figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">jsp-api</span></span><br><span class="line"><span class="keyword">jstl</span></span><br><span class="line"><span class="keyword">servlet-api</span></span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>导包（不可导错）</p>
<p><img src="https://i.loli.net/2021/03/18/bpVa9CXe3iPmJf8.png" alt="image-20210318133409267"></p>
</li>
<li><p>编写一个一定会中文乱码的servlet类</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">ServletDemo1</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doGet(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        resp.getWriter().write(<span class="string">&quot;你好&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> void doPost(<span class="type">HttpServletRequest</span> req, <span class="type">HttpServletResponse</span> resp) <span class="keyword">throws</span> <span class="type">ServletException</span>, <span class="type">IOException</span> &#123;</span><br><span class="line">        doGet(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写filter类</p>
<p>注意：</p>
<p><code>chain</code>链：</p>
<ul>
<li><p>过滤中所有代码中，在特定过滤请求中的时候会执行</p>
</li>
<li><p>必须让所有过滤器继续同行</p>
<p>chain.doFilter(request,response);</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.liefox.filter;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.*;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * projectName: Java_Web-03</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: 佐佳豪</span></span><br><span class="line"><span class="comment"> * time: 2021/3/18 下午 1:30</span></span><br><span class="line"><span class="comment"> * description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FilterDemo1</span> <span class="keyword">implements</span> <span class="title">Filter</span> </span>&#123;</span><br><span class="line">    <span class="comment">//初始化</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(FilterConfig filterConfig)</span> <span class="keyword">throws</span> ServletException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;初始化&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doFilter</span><span class="params">(ServletRequest request, ServletResponse response, FilterChain chain)</span> <span class="keyword">throws</span> IOException, ServletException </span>&#123;</span><br><span class="line">        <span class="comment">//解决中文乱码</span></span><br><span class="line">        request.setCharacterEncoding(<span class="string">&quot;utf-8&quot;</span>);</span><br><span class="line">        response.setCharacterEncoding(<span class="string">&quot;utf-8&quot;</span>);</span><br><span class="line">        response.setContentType(<span class="string">&quot;text/html;charset=utf-8&quot;</span>);</span><br><span class="line">        chain.doFilter(request,response);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//销毁，关闭服务器就会销毁</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroy</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;已销毁&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写映射文件</p>
<figure class="highlight dts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="params">&lt;servlet&gt;</span></span><br><span class="line">       <span class="params">&lt;servlet-name&gt;</span>ServletDemo1<span class="params">&lt;/servlet-name&gt;</span></span><br><span class="line">       <span class="params">&lt;servlet-class&gt;</span>com.liefox.servlet.ServletDemo1<span class="params">&lt;/servlet-class&gt;</span></span><br><span class="line">   <span class="params">&lt;/servlet&gt;</span></span><br><span class="line">   <span class="params">&lt;servlet-mapping&gt;</span></span><br><span class="line">       <span class="params">&lt;servlet-name&gt;</span>ServletDemo1<span class="params">&lt;/servlet-name&gt;</span></span><br><span class="line">       <span class="params">&lt;url-pattern&gt;</span><span class="meta-keyword">/servlet/</span>s1<span class="params">&lt;/url-pattern&gt;</span><span class="comment">//配置一个过滤器会过滤的路径</span></span><br><span class="line">   <span class="params">&lt;/servlet-mapping&gt;</span></span><br><span class="line">   <span class="params">&lt;servlet-mapping&gt;</span></span><br><span class="line">       <span class="params">&lt;servlet-name&gt;</span>ServletDemo1<span class="params">&lt;/servlet-name&gt;</span></span><br><span class="line">       <span class="params">&lt;url-pattern&gt;</span>/s1<span class="params">&lt;/url-pattern&gt;</span><span class="comment">//配置一个过滤器不会过滤的路径</span></span><br><span class="line">   <span class="params">&lt;/servlet-mapping&gt;</span></span><br><span class="line">   <span class="comment">////配置过滤器</span></span><br><span class="line">   <span class="params">&lt;filter&gt;</span></span><br><span class="line">       <span class="params">&lt;filter-name&gt;</span>FilterDemo1<span class="params">&lt;/filter-name&gt;</span></span><br><span class="line">       <span class="params">&lt;filter-class&gt;</span>com.liefox.filter.FilterDemo1<span class="params">&lt;/filter-class&gt;</span></span><br><span class="line">   <span class="params">&lt;/filter&gt;</span></span><br><span class="line">   <span class="params">&lt;filter-mapping&gt;</span></span><br><span class="line">       <span class="params">&lt;filter-name&gt;</span>FilterDemo1<span class="params">&lt;/filter-name&gt;</span></span><br><span class="line">       <span class="params">&lt;url-pattern&gt;</span><span class="meta-keyword">/servlet/</span>*<span class="params">&lt;/url-pattern&gt;</span><span class="comment">//配置过滤器过滤的路径</span></span><br><span class="line">   <span class="params">&lt;/filter-mapping&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>访问不过滤的，乱码</p>
<p><img src="https://i.loli.net/2021/03/18/lGw45WxdCQFP3Kv.png" alt="image-20210318142145522"></p>
</li>
<li><p>访问过滤的，正常</p>
<p><img src="https://i.loli.net/2021/03/18/uovsFMWfkTtU8nR.png" alt="image-20210318142232279"></p>
</li>
</ul>
<h2 id="监听器"><a href="#监听器" class="headerlink" title="监听器"></a>监听器</h2><p>除了Servlet和Filter外，JavaEE的Servlet规范还提供了第三种组件：Listener。</p>
<p>Listener顾名思义就是监听器，有好几种Listener，其中最常用的是<code>ServletContextListener</code>，我们编写一个实现了<code>ServletContextListener</code>接口的类如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@WebListener</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AppListener</span> <span class="keyword">implements</span> <span class="title">ServletContextListener</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 在此初始化WebApp,例如打开数据库连接池等:</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextInitialized</span><span class="params">(ServletContextEvent sce)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;WebApp initialized.&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在此清理WebApp,例如关闭数据库连接池等:</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextDestroyed</span><span class="params">(ServletContextEvent sce)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;WebApp destroyed.&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>任何标注为<code>@WebListener</code>，且实现了特定接口的类会被Web服务器自动初始化。上述<code>AppListener</code>实现了<code>ServletContextListener</code>接口，它会在整个Web应用程序初始化完成后，以及Web应用程序关闭后获得回调通知。我们可以把初始化数据库连接池等工作放到<code>contextInitialized()</code>回调方法中，把清理资源的工作放到<code>contextDestroyed()</code>回调方法中，因为Web服务器保证在<code>contextInitialized()</code>执行后，才会接受用户的HTTP请求。</p>
<p>很多第三方Web框架都会通过一个<code>ServletContextListener</code>接口初始化自己。</p>
<p>除了<code>ServletContextListener</code>外，还有几种Listener：</p>
<ul>
<li>HttpSessionListener：监听HttpSession的创建和销毁事件；</li>
<li>ServletRequestListener：监听ServletRequest请求的创建和销毁事件；</li>
<li>ServletRequestAttributeListener：监听ServletRequest请求的属性变化事件（即调用<code>ServletRequest.setAttribute()</code>方法）；</li>
<li>ServletContextAttributeListener：监听ServletContext的属性变化事件（即调用<code>ServletContext.setAttribute()</code>方法）；</li>
</ul>
<h3 id="ServletContext-1"><a href="#ServletContext-1" class="headerlink" title="ServletContext"></a>ServletContext</h3><p>一个Web服务器可以运行一个或多个WebApp，对于每个WebApp，Web服务器都会为其创建一个全局唯一的<code>ServletContext</code>实例，我们在<code>AppListener</code>里面编写的两个回调方法实际上对应的就是<code>ServletContext</code>实例的创建和销毁：</p>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public void context<span class="constructor">Initialized(ServletContextEvent <span class="params">sce</span>)</span> &#123;</span><br><span class="line">    <span class="module-access"><span class="module"><span class="identifier">System</span>.</span></span>out.println(<span class="string">&quot;WebApp initialized: ServletContext = &quot;</span> + sce.get<span class="constructor">ServletContext()</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>ServletRequest</code>、<code>HttpSession</code>等很多对象也提供<code>getServletContext()</code>方法获取到同一个<code>ServletContext</code>实例。<code>ServletContext</code>实例最大的作用就是设置和共享全局信息。</p>
<p>此外，<code>ServletContext</code>还提供了动态添加Servlet、Filter、Listener等功能，它允许应用程序在运行期间动态添加一个组件，虽然这个功能不是很常用。</p>
<ul>
<li>通过Listener我们可以监听Web应用程序的生命周期，获取<code>HttpSession</code>等创建和销毁的事件；</li>
<li><code>ServletContext</code>是一个WebApp运行期的全局唯一实例，可用于设置和共享配置信息。</li>
</ul>
<hr>
<p>作者：zjh</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本网站所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://cxq21.gitee.io/posts/f1404f68.html" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JAVA%E7%AC%94%E8%AE%B0/" rel="tag">JAVA笔记</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/posts/c189ba0e.html" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            JavaWeb项目-网站拓展
          
        </div>
      </a>
    
    
      <a href="/posts/bd30505d.html" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">将网站打包成webApp</div>
      </a>
    
  </nav>

  
      
<!-- minivaline评论 -->
<div id="mvcomments-box">
  <div id="mvcomments"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/minivaline@3/dist/MiniValine.min.js"></script>
<script>
    new MiniValine({
        el: '#mvcomments',
        appId: 'lGCocPMCSUz2tL4FcKDvFCG3-gzGzoHsz',
        appKey: 'VheJztjyMzs24xKQPGGI1bmh',
        mode: 'xCss',
        placeholder: '评论的时候建议填上您的邮箱，可以第一时间收到我的回复喔',
        pathname: window.location.pathname,
        lang: '',
        adminEmailMd5: '69b690b67d0fb9dba8ab2de5db6dd353',
        tagMeta: ["LIEFox站长", "小伙伴", "来将可留姓名"],
        master: ["69b690b67d0fb9dba8ab2de5db6dd353"],
        friends: ["1f7c8b18fb7bbe18fbaf97309de2c0ac", "e5aec373ad8a0dce7abdbe4e6585a8ba"],
        math: true,
        md: true,
        enableQQ: true,
        NoRecordIP: true,
        visitor: false,
        maxNest: 6,
        pageSize: 6,
        serverURLs: '',
        emoticonUrl: ["https://cdn.jsdelivr.net/npm/alus@latest", "https://cdn.jsdelivr.net/gh/MiniValine/qq@latest", "https://cdn.jsdelivr.net/gh/MiniValine/Bilibilis@latest", "https://cdn.jsdelivr.net/gh/MiniValine/tieba@latest", "https://cdn.jsdelivr.net/gh/MiniValine/twemoji@latest", "https://cdn.jsdelivr.net/gh/MiniValine/weibo@latest"],
    });
  const infoEle = document.querySelector('#mvcomments .info');
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
      infoEle.childNodes.forEach(function (item) {
          item.parentNode.removeChild(item);
      });
  }
</script>
<style>
	#mvcomments-box {
	padding: 5px 30px;
	}

	@media screen and (max-width: 800px) {
	#mvcomments-box {
	  padding: 5px 0px;
	}
	}

	.v .vlist .vcard .vh {
	padding-right: 20px;
	}

	.v .vlist .vcard {
	padding-left: 10px;
	}
	
	.darkmode .commentTrigger{
		background-color: #403e3e !important;
	  }
	.darkmode .MiniValine .vpage .more{
		background: #21232F
	}
	.darkmode img{
		  filter: brightness(30%)
	}
	.darkmode .MiniValine .vlist .vcard .vcomment-body .text-wrapper .vcomment.expand:before{
		background: linear-gradient(180deg, rgba(246,246,246,0), rgba(0,0,0,0.9))
	}
	.darkmode .MiniValine .vlist .vcard .vcomment-body .text-wrapper .vcomment.expand:after{
		background: rgba(0,0,0,0.9)
	}
	.darkmode .MiniValine .vlist .vcard .vcomment-body .text-wrapper .vcomment pre{
		background: #282c34
		border: 1px solid #282c34
	}
	.darkmode .MiniValine .vinputs-area .textarea-wrapper textarea{
		color: #000;
	}
	.darkmode .MiniValine .vinputs-area .auth-section .input-wrapper input{
		color: #000;
	}
	.darkmode .MiniValine .info .col .count{
		color: #000;
	}
	.darkmode .MiniValine .vinputs-area .vextra-area .vsmile-icons{
		background: transparent;
	}
</style>

 
</article>

</section>
      <footer class="footer">
  <div class="outer">
<!-- 运行天数 -->
    <ul>
        <li><span id="runtime_span"></span></li>
    </ul>

<script type="text/javascript">            
    function show_runtime() {
        window.setTimeout("show_runtime()", 1000);
        X = new Date("11/27/2020 17:17:17");
        Y = new Date();
        T = (Y.getTime() - X.getTime());
        M = 24 * 60 * 60 * 1000;
        a = T / M;
        A = Math.floor(a);
        b = (a - A) * 24;
        B = Math.floor(b);
        c = (b - B) * 60;
        C = Math.floor((b - B) * 60);
        D = Math.floor((c - C) * 60);
        runtime_span.innerHTML = "LIEFox逃脱猎人的第" + A + "天" + B + "小时" + C + "分" + D + "秒"
    }
    show_runtime();
</script>

    <ul>
      <li>
        Copyrights &copy;
        2020-2023
        <i class="ri-heart-fill heart_icon"></i> 拾柒工作室
      </li>
    </ul>


    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1279472270&amp;web_id=1279472270'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/LIEFox.png" alt="LIEFox"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">时间线</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/dplayer">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/update">更新日志</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于LIEFox</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>感谢金主~</p>
  <div class="reward-box">
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>